import time
from datetime import datetime
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
if __name__ == '__main__':
    service = Service('G:\Python\python38\chromedriver.exe')
    driver = webdriver.Chrome(service=service)
    driver.get("http://localhost:90/")
    driver.maximize_window()

    username='lisi'
    password='123456'
    driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(username)
    driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
    driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@id="bankCard_add_modal_btn"]').click()

    # 弹出了一个新增银行卡的模态框
    # 等待模态框中的按钮可点击
    cardBank='天地银行'
    kind='信用卡'
    cardNum='1234567891548796'
    try:
        # 银行名称
        input_cardBank = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="cardBank_add_input"]'))
        )
        input_cardBank.send_keys(cardBank)
        # 卡类型
        # 等待模态框中的单选按钮（借记卡）可点击
        if kind == '借记卡':
            radio = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable(
                    (By.XPATH, '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]'))
            )
            # 点击单选按钮（借记卡）
            radio.click()
        else:
            radio = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable(
                    (By.XPATH, '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]'))
            )
            # 点击单选按钮（信用卡）
            radio.click()
        # 银行卡号
        input_cardNum = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="cardNum_add_input"]'))
        )
        input_cardNum.send_keys(cardNum)
        # 保存按钮
        save_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="bankCard_save_btn"]'))
        )
        save_button.click()
        formatted_now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
        print(formatted_now)

        driver.save_screenshot(
            "G:/PycharmProjects/python_test_script/automate_testing/error_img/insert_card_"  + ".png")
    except  Exception as e:
        print(e)
    finally:
        driver.quit()


